package com.carepad.action.password;

import org.springframework.beans.factory.annotation.Autowired;

import com.carepad.action.AbstractAction;
import com.carepad.service.impl.EmailService;
import com.carepad.util.EmailUtil;

public class PasswordForgetAction extends AbstractAction {

	private static final long serialVersionUID = 1L;

	private String emailAddress;
	private String forgetPasswordStatus;
	
	@Autowired private EmailService emailService;
	
	public String passwordForgetRequest() {
		return SUCCESS;
	}
	
	public String forgetPassword() throws Exception {
		
		setForgetPasswordStatus("ERROR");
		
		if(emailAddress != null && !emailAddress.isEmpty()) {
		
			if(EmailUtil.chackEmailPattern(emailAddress)) {

				String key = emailService.sendEmail(emailAddress);
				addActionError(getText(key));
				
				if(key.startsWith("error")) {
					return INPUT;
				} else {
					setForgetPasswordStatus("SUCCESS");
					return SUCCESS;
				}

			} else {
				addActionError(getText("error.email.invalid"));
				return INPUT;
			}

		} else {
			addActionError(getText("error.email.empty"));
			return INPUT;
		}
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getForgetPasswordStatus() {
		return forgetPasswordStatus;
	}

	public void setForgetPasswordStatus(String forgetPasswordStatus) {
		this.forgetPasswordStatus = forgetPasswordStatus;
	}

}
